package datamining.core.main;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import datamining.associations.apriori.AprioriOutput;
import datamining.code.dao.EmprestimoDAO;
import datamining.code.model.Emprestimo;
import datamining.code.model.Transaction;

public class Weka {
	static EmprestimoDAO dao = new EmprestimoDAO();
	
	public static void main(String[] args) throws IOException {		
		try {
			ArrayList<Object> list = dao.retrieve();
			ArrayList<Transaction> transactions = processTransaction(list);
			dateCheck(transactions);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public static ArrayList<Transaction> processTransaction(ArrayList<Object> list) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
		ArrayList<Transaction> transactions = new ArrayList<Transaction>();
		for (Object object : list) {
			Emprestimo emp = (Emprestimo) object;
			Transaction transaction = new Transaction();
			try {
				transaction.setCodpublicacao(emp.getCodpublicacao());
				transaction.setTipo(emp.getTipo());
				transaction.setCodigo(emp.getCodigo());
				transaction.setCategoria(emp.getCategoria());
				transaction.setDtReserva(emp.getDtReserva());
				transaction.setDtDevEfetiva(simpleDateFormat.parse(emp.getDtDevEfetiva()));
				transaction.setDtEmprestimo(simpleDateFormat.parse(emp.getDtEmprestimo()));
				transaction.setDtDevPrevista(simpleDateFormat.parse(emp.getDtDevPrevista()));
				transactions.add(transaction);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
		return transactions;
	}
	
	public static void dateCheck(ArrayList<Transaction> transactions) {
		for (Transaction transaction : transactions) {
			if (transaction.getDtDevEfetiva().after(transaction.getDtDevPrevista()))
				transaction.setDevolucao("atrasado");
			else if (transaction.getDtDevEfetiva().before(transaction.getDtDevPrevista()))
				transaction.setDevolucao("adiantado");
			else if (transaction.getDtDevEfetiva().equals(transaction.getDtDevPrevista()))
				transaction.setDevolucao("exato");			
		}
		processSubject(transactions);
	}
	
	private static void processSubject(ArrayList<Transaction> transactions) {
		for (Transaction transaction : transactions) {
			transaction.setAssunto(dao.retrieveSubject(transaction.getCodpublicacao()));
		}
		try {
			writeFile(transactions);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void writeFile(ArrayList<Transaction> transactions)
			throws IOException {
		
		//copy the header from header file
		File inputFile = new File("arff/header.arff");
		File outputFile = new File("arff/weka.arff");
		FileReader in = new FileReader(inputFile);
		FileWriter out = new FileWriter(outputFile);
		int c;
		while ((c = in.read()) != -1)
			out.write(c);
		in.close();
		
		//write data section
		BufferedWriter bufferedWriter = new BufferedWriter(out);
		for (Transaction transaction : transactions) {
			String line = "";
			line += transaction.getCategoria() + ",";
			line += transaction.getAssunto() + ",";
			line += transaction.getDevolucao() + ",";
			if (transaction.getDevolucao().equals(""))
				line += "emprestimo,";
			else
				line += "reserva,";
			line += transaction.getTipo() + "\r";
			bufferedWriter.append(line);
		}
		bufferedWriter.flush();
		bufferedWriter.close();
		try {
			AprioriOutput.Run(outputFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
